/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.noah.rowmapper;

import com.noah.model.RoleModel;
import com.noah.model.UserModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author ngoc.phan
 */
public class UserRowMapper implements RowMapper {

    private JdbcTemplate jdbctemplate;

    public UserRowMapper() {
    }

    public UserRowMapper(JdbcTemplate jdbctemplate) {
        this.jdbctemplate = jdbctemplate;
    }

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        
        UserModel user = new UserModel();
        user.setUserName(rs.getString("userName"));
        user.setPassword(rs.getString("password"));
        user.setUserId(rs.getInt("userId"));
        String sql = "select * from role where roleId=?";
        RoleModel role = (RoleModel) jdbctemplate.queryForObject(sql, new Object[]{rs.getInt("RoleId")}, new RoleRowMapper());
        user.setRole(role);
        //if(rs.getInt("RoleId") != 0){
        // RoleModel role =(RoleModel) jdbctemplate.queryForObject(sql, new Object[]{rs.getInt("RoleId")}, new RoleRowMapper());
        //user.setRole(role);
        // }
        //loginForm.setRoleId(rs.getInt("RoleId"));
        //loginForm.setRoleName(rs.getString("RoleName"));
        return user;
    }
}
